﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ipaidabribe
{
    public class RewritingHttpModule : IHttpModule
    {
        #region IHttpModule Members

        public void Dispose()
        {
        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += Context_BeginRequest;

        }
        private static void Context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication httpApplication = (HttpApplication)sender;            
            string url = httpApplication.Request.RawUrl.ToLower();            

            //see if we have a match
            var arr1 = url.Split('-');
            var id = arr1[arr1.Count() - 1].Replace(".aspx", "");

            int myId = 0;
            bool b = int.TryParse(id, out myId);
            if (b)
            {
                SendToNewUrl(String.Format("/Detail.aspx?id={0}", id), httpApplication);
            }
        }
        public static void SendToNewUrl(string url, System.Web.HttpApplication appl)
        {

            appl.Context.RewritePath(url);

        }
        #endregion
    }
}